package udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

import java.net.InetSocketAddress;


public class Server {
    private final Bootstrap bootstrap;
    private final String msg;
    private final EventLoopGroup group;


    public Server(InetSocketAddress address, String msg) {
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();

        bootstrap.group(group)
                .channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(new ServerEventEncoder(address));
        this.msg = msg;

    }

    public void bind(int port) {

        Channel channel = bootstrap.bind(port).syncUninterruptibly().channel();

//        while (true) {
//            try {
//                if (!"".equals(Data.getData())) {
//                    channel.writeAndFlush(Data.getData());
//                    System.out.println("发送消息:" + Data.getData());
//
//                    Data.setData("");
//                }
//                Thread.sleep(1);
//
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } finally {
//                group.shutdownGracefully();
//            }

//        }


    }

    public static void main(String[] args) {


        new Server(new InetSocketAddress("255.255.255.255", 9999), "电磁阀时开个包括句话").bind(0);

    }
}
